import os
import time
import uuid
import datetime
import random


class Demo(object):

    def __init__(self):
        f = open(r"D:\Disktop\id.txt", 'r')
        self.id_list = f.readlines()
        f.close()

    def craete_file_1(self, dir_path, fileName, contentType, number, szStoreID):
        for i in range(number):
            szName = self.get_file_name(fileName)
            content = self.get_file_content(contentType, szStoreID)
            self.writeFile(dir_path, szName, content)

    def get_file_content(self, contentType, szStoreID, ):
        if contentType == '1':
            szDate = self.getDate()
            szTime = self.getTime()
            szDocumentNmbr = szDate + str(random.randint(1000000000, 9999999999))
            file_Connect_0 = '<?xml version="1.0" encoding="UTF-8"?><MTStoAdjust><IDOC BEGIN="1"><Header><szBusinessDate><![CDATA['+szDate+']]></szBusinessDate><szTime><![CDATA['+ szTime +']]></szTime><lWorkstationNmbr><![CDATA[999]]></lWorkstationNmbr><FILIALE><![CDATA['+ szStoreID +']]></FILIALE><szRetailStoreExternalID><![CDATA['+ szStoreID +']]></szRetailStoreExternalID><szDocumentNmbr><![CDATA['+ szDocumentNmbr +']]></szDocumentNmbr><szReasonCode><![CDATA[27]]></szReasonCode><szDocType><![CDATA[7]]></szDocType></Header>'
            file_Connect_1 = '<Item><szStorageLocationID><![CDATA[0001]]></szStorageLocationID><szPOSItemID><![CDATA['+ self.getID() +']]></szPOSItemID><dQty><![CDATA[3]]></dQty><lSequenceNumber><![CDATA[]]></lSequenceNumber></Item>'
            file_Connect_2 = '</IDOC></MTStoAdjust>'
            for i in range(100):
                file_Connect_1 = file_Connect_1 + '<Item><szStorageLocationID><![CDATA[0001]]></szStorageLocationID><szPOSItemID><![CDATA['+ self.getID() +']]></szPOSItemID><dQty><![CDATA[3]]></dQty><lSequenceNumber><![CDATA[]]></lSequenceNumber></Item>'
            return file_Connect_0 + file_Connect_1 + file_Connect_2

        if contentType == "2":
            szExternalDocumentDate = self.getDate()
            szExternalDocumentNmbr = "O" + str(random.randint(100000000, 999999999))
            szDocumentNmbr = "BOL210" + str(random.randint(100000000, 999999999))

            lSequenceNumber = 10

            file_Connect_0 = '<?xml version="1.0" encoding="UTF-8"?><ns0:MT_ISPOrder xmlns:ns0="urn:aldi.com:iris_retail:poandsto"><Header><szDocType><![CDATA[Z001]]></szDocType><szExternalDocumentNmbr><![CDATA['+ szExternalDocumentNmbr +']]></szExternalDocumentNmbr><szExternalPurchaseGroup><![CDATA[]]></szExternalPurchaseGroup><szExternalDocumentDate><![CDATA['+ szExternalDocumentDate +']]></szExternalDocumentDate><szVendorID><![CDATA[0000105193]]></szVendorID><szDocumentNmbr><![CDATA['+ szDocumentNmbr +']]></szDocumentNmbr><ZCOTYPE><![CDATA[]]></ZCOTYPE><channel><![CDATA[]]></channel></Header>'
            file_Connect_1 = '<Item><szRetailStoreExternalIDFrom/><lSequenceNumber><![CDATA['+ str(lSequenceNumber).ljust(6,'0') +']]></lSequenceNumber><szStorageLocationID><![CDATA[0001]]></szStorageLocationID><dQty><![CDATA[100.000]]></dQty><szPriceUnitOfMeasureName><![CDATA[]]></szPriceUnitOfMeasureName><FILIALE><![CDATA['+szStoreID+']]></FILIALE><szExpectedDeliveryDate><![CDATA['+ szExternalDocumentDate +']]></szExpectedDeliveryDate><szItemID><![CDATA['+ self.getID() +']]></szItemID></Item>'
            file_Connect_2 = '</ns0:MT_ISPOrder>'
            for i in range(100):
                lSequenceNumber = lSequenceNumber + 10
                file_Connect_1 = file_Connect_1 + '<Item><szRetailStoreExternalIDFrom/><lSequenceNumber><![CDATA['+ str(lSequenceNumber).ljust(6,'0') +']]></lSequenceNumber><szStorageLocationID><![CDATA[0001]]></szStorageLocationID><dQty><![CDATA[100.000]]></dQty><szPriceUnitOfMeasureName><![CDATA[]]></szPriceUnitOfMeasureName><FILIALE><![CDATA['+szStoreID+']]></FILIALE><szExpectedDeliveryDate><![CDATA['+ szExternalDocumentDate +']]></szExpectedDeliveryDate><szItemID><![CDATA['+ self.getID() +']]></szItemID></Item>'
            return file_Connect_0 + file_Connect_1 + file_Connect_2

        if contentType == "3":
            szDate = self.getDate()
            # szStoreID = '1008'
            szExternalDocumentNmbr = str(random.randint(2100000000000000, 9999999999999999))
            file_Connect_0 = '<?xml version="1.0" encoding="UTF-8"?><ns0:SOQISP_MT xmlns:ns0="urn:aldi.com:iris_retail:forecastandsoq">'
            file_Connect_1 = '<Record><szDate><![CDATA['+ szDate +']]></szDate><FILIALE><![CDATA['+ szStoreID +']]></FILIALE><szItemID><![CDATA['+ self.getID() +']]></szItemID><szDescription><![CDATA[]]></szDescription><dMinimum><![CDATA[0]]></dMinimum><dMaximum><![CDATA[]]></dMaximum><dSOQ>0</dSOQ><dBOQ>0</dBOQ><szABCIndicator><![CDATA[1]]></szABCIndicator><szExternalDocumentNmbr><![CDATA['+ szExternalDocumentNmbr +']]></szExternalDocumentNmbr><dSafeStock><![CDATA[0]]></dSafeStock><dYesterdaySalesNum><![CDATA[]]></dYesterdaySalesNum></Record>'
            file_Connect_2 = '</ns0:SOQISP_MT>'
            for i in range(206):
                file_Connect_1 = file_Connect_1 + '<Record><szDate><![CDATA['+ szDate +']]></szDate><FILIALE><![CDATA['+ szStoreID +']]></FILIALE><szItemID><![CDATA['+ self.getID() +']]></szItemID><szDescription><![CDATA[]]></szDescription><dMinimum><![CDATA[0]]></dMinimum><dMaximum><![CDATA[]]></dMaximum><dSOQ>0</dSOQ><dBOQ>0</dBOQ><szABCIndicator><![CDATA[1]]></szABCIndicator><szExternalDocumentNmbr><![CDATA['+ szExternalDocumentNmbr +']]></szExternalDocumentNmbr><dSafeStock><![CDATA[0]]></dSafeStock><dYesterdaySalesNum><![CDATA[]]></dYesterdaySalesNum></Record>'
            return file_Connect_0 + file_Connect_1 + file_Connect_2

        if contentType == "4":
            # szStoreID = '1008'
            szDate = ''
            RULEIDENTIFIERID = str(random.randint(2100000000000000, 9999999999999999))
            sku_1 = self.getID()
            sku_2 = self.getID()
            return '<?xml version="1.0" encoding="UTF-8"?><WPDBBY01><IDOC BEGIN="1"><E1WPBB01 SEGMENT="1"><AENDKENNZ>MODI</AENDKENNZ><PRIORITY><![CDATA[100]]></PRIORITY><MAXCALCNMBR><![CDATA[0]]></MAXCALCNMBR><ONDISCOUNTEDITEMS><![CDATA[0]]></ONDISCOUNTEDITEMS><AKTIVDATUM><![CDATA[2021-11-12 10:42:01]]></AKTIVDATUM><FILIALE><![CDATA['+szStoreID+']]></FILIALE><BBY_NR><![CDATA['+RULEIDENTIFIERID+']]></BBY_NR><RULEIDENTIFIERID><![CDATA['+RULEIDENTIFIERID+']]></RULEIDENTIFIERID><E1WPBB03><DISC_TYPE><![CDATA[F]]></DISC_TYPE><DISC_VALUE><![CDATA[5.00]]></DISC_VALUE><E1WPBB04><PRQ_TYPE>PLU</PRQ_TYPE><ARTIKELNR_BEGIN><![CDATA['+sku_1+']]></ARTIKELNR_BEGIN><ARTIKELNR_END><![CDATA['+sku_1+']]></ARTIKELNR_END><E1WPBB06><DISC_REQTYPE>DQTY</DISC_REQTYPE><DISC_REQVALUE><![CDATA[4.00]]></DISC_REQVALUE></E1WPBB06></E1WPBB04><E1WPBB04><PRQ_TYPE>PLU</PRQ_TYPE><ARTIKELNR_BEGIN><![CDATA['+sku_2+']]></ARTIKELNR_BEGIN><ARTIKELNR_END><![CDATA['+sku_2+']]></ARTIKELNR_END><E1WPBB06><DISC_REQTYPE>DQTY</DISC_REQTYPE><DISC_REQVALUE><![CDATA[1.00]]></DISC_REQVALUE></E1WPBB06></E1WPBB04></E1WPBB03><E1WPBB05><COND_TYPE>MxMVarPrice</COND_TYPE><START_DATE>20211112</START_DATE><START_TIME>10:38:00</START_TIME><END_DATE>20211113</END_DATE><END_TIME>10:39:00</END_TIME><E1WPBB10><ONSUNDAY>-1</ONSUNDAY><ONMONDAY>-1</ONMONDAY><ONTUESDAY>-1</ONTUESDAY><ONWEDNESDAY>-1</ONWEDNESDAY><ONTHURSDAY>-1</ONTHURSDAY><ONFRIDAY>-1</ONFRIDAY><ONSATURDAY>-1</ONSATURDAY></E1WPBB10></E1WPBB05><E1WPBB07><SHORT_TEXT><![CDATA[固定组合促销验bug]]></SHORT_TEXT></E1WPBB07></E1WPBB01></IDOC></WPDBBY01>'

        if contentType == "5":
            return '<?xml version="1.0" encoding="UTF-8"?><TIME_PRICE__EXT><IDOC><E1WBB07><ACTYPE>MODI</ACTYPE><FILIALE>'+ szStoreID +'</FILIALE><MATNR><![CDATA['+ self.getID() +']]></MATNR><MAKTM><![CDATA[商品服务]]></MAKTM><DATAB><![CDATA[20211116]]></DATAB><DATBI><![CDATA[99991231]]></DATBI><KWERT><![CDATA[5.00]]></KWERT></E1WBB07></IDOC></TIME_PRICE__EXT>'

        if contentType == "6":
            return '<?xml version="1.0" encoding="UTF-8"?><UPDSTA><IDOC><ITSTA01><FILIALE>'+ szStoreID +'</FILIALE><ARTIKELNR>'+ self.getID() +'</ARTIKELNR><BLOCKED></BLOCKED><ZLFRHY>ALL</ZLFRHY><ZCSIZE></ZCSIZE></ITSTA01></IDOC></UPDSTA>'

        if contentType == '7':
            sku = self.getID()
            uid = str(uuid.uuid1()).replace("-", "")
            return f'<?xml version="1.0" encoding="UTF-8"?><WP_PLU02_EXT><IDOC BEGIN="1"><E1WPA01 SEGMENT="1"><OrderNo><![CDATA[20220406095500-{sku}-{uid}]]></OrderNo><FILIALE>{storeID}</FILIALE><AENDKENNZ><![CDATA[INIT]]></AENDKENNZ><AKTIVDATUM><![CDATA[20220406]]></AKTIVDATUM><HAUPTEAN><![CDATA[578485845455]]></HAUPTEAN><ARTIKELNR><![CDATA[{sku}]]></ARTIKELNR><POSME><![CDATA[PC]]></POSME><BLOCKED><![CDATA[]]></BLOCKED><E1WPA02 SEGMENT="E1WPA02"><WARENGR><![CDATA[0101]]></WARENGR><WARENGH><![CDATA[100004]]></WARENGH><WARENGS><![CDATA[100004]]></WARENGS><ARTIKANZ></ARTIKANZ><MHDHB><![CDATA[55]]></MHDHB></E1WPA02><E1WPA03 SEGMENT="1"><QUALARTTXT><![CDATA[LTXT]]></QUALARTTXT><TEXT><![CDATA[鲁西黄牛牛腿肉80g-{uid}]]>&gt;</TEXT></E1WPA03><E1WPA04 SEGMENT="1"><KONDART></KONDART><BEGINDATUM><![CDATA[20220405]]></BEGINDATUM><BEGINNZEIT><![CDATA[20220405000000]]></BEGINNZEIT><ENDDATUM><![CDATA[20220405]]></ENDDATUM><ENDZEIT><![CDATA[20220405000000]]></ENDZEIT><E1WPA05 SEGMENT="1"><VORZEICHEN></VORZEICHEN><KONDWERT><![CDATA[1]]></KONDWERT><MENGE></MENGE><CURRENCY></CURRENCY></E1WPA05></E1WPA04><E1WPA07 SEGMENT="1"><MWSKZ><![CDATA[6]]></MWSKZ></E1WPA07><E1WXX01 SEGMENT=""><FLDNAME>ZPRSLOC</FLDNAME><FLDVAL><![CDATA[0001]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZORDTYP</FLDNAME><FLDVAL><![CDATA[1]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZSTCKALL</FLDNAME><FLDVAL><![CDATA[Y]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZBWSCL</FLDNAME><FLDVAL><![CDATA[1]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZMAABC</FLDNAME><FLDVAL><![CDATA[B]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZLFRHY</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>SSTOCK</FLDNAME><FLDVAL><![CDATA[0.000]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZMINQTY</FLDNAME><FLDVAL><![CDATA[0]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZSALDATUM</FLDNAME><FLDVAL><![CDATA[11]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>SALEBLOCKDATE</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZARTTYPE</FLDNAME><FLDVAL><![CDATA[Z001]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>BSCALEITEMFLAG</FLDNAME><FLDVAL><![CDATA[N]]></FLDVAL></E1WXX01></E1WPA01></IDOC></WP_PLU02_EXT>'

    # 获取文件名字
    def get_file_name(self, fileFinal):
        file_name_uuid = str(uuid.uuid1()).replace("-", "")
        return fileFinal + "_" + self.getDateTime() + "_" + file_name_uuid

    # 获取日期
    def getDate(self):
        return self.getDateTime()[:8].replace("-", "")

    # 获取时间
    def getTime(self):
        return self.getDateTime()[11:].replace(":", "")

    # 获取当前日期时间
    def getDateTime(self):
        now = datetime.datetime.now()
        file_name_datetime = datetime.datetime.strptime(str(now)[:-7], "%Y-%m-%d %H:%M:%S")
        file_name_datetime = str(file_name_datetime).replace("-", "").replace(":", "").replace(" ", "")
        return file_name_datetime

    # 随机获取一个商品
    def getID(self):
        return self.id_list[random.randint(0,len(self.id_list)-1)].replace("\n", "")

    # 写文件
    def writeFile(self, dir_path, fileName, content):
        abs_path = dir_path + "\\" + fileName + ".xml"
        f = open(abs_path, "a", encoding='utf8')
        f.write(content)
        f.close()

    def monitor_file(self, dir_path):
        while True:
            for root, dir, files in os.walk(dir_path):
                print(len(files))
                print(files)
                time.sleep(1)


if __name__ == '__main__':
    demo = Demo()
    base_path = r"D:\Disktop\1"
    # demo.monitor_file(base_path)
    number = 20000

    # storeID_list = ["1002", '1003', '1005', '1008', '1026']
    # storeID_list = ["1002", '1003', '1008']
    storeID_list = ['1003']
    for storeID in storeID_list:
        # demo.craete_file_1(base_path, "WP", "1", number, storeID)
        demo.craete_file_1(base_path, "WPMT_ISPOrder", "2", number, storeID)
        demo.craete_file_1(base_path, "WPSOQISP_MT", "3", number, storeID)
        demo.craete_file_1(base_path, "WPDBBY01", "4", number, storeID)
        demo.craete_file_1(base_path, f"WPTIME_PRICE__EXT_{storeID}", "5", number, storeID)
        demo.craete_file_1(base_path, f"WPUPDSTA_{storeID}", "6", number, storeID)
        # 商品主档
        # demo.craete_file_1(base_path, f"WP_PLU02_EXT_{storeID}", "7", number, storeID)
